****************************************************************************
*``The Costs and Environmental Justice Concerns of NIMBY in Solid Waste Disposal''*
* Phuong Ho (SNF-Centre for Applied Research in Norwegian School of Economics) *
*******************************************************************************
*** This file makes Table 5 and Table 6 ***********
cls
clear all 
capture log close
set more off

global mydir "C:\XYZ" // set working directory
global data "$mydir\data"
global matlabin "$mydir\matlab\input"
global result "$mydir\TEX\"
cd "$mydir\stata"
set matsize 11000
set maxvar 32700

***********************************************************************
**** IMPORT STATUS QUO ESTIMATES *************************************
**********************************************************************
import excel "$mydir\matlab\output\nimby_statusquo.xlsx", sheet("Sheet1") firstrow clear
label var sj_q "market size"
label var pr "model implied market share"
label var Bq "model implied trash amount"
label var el "price elasticity"
label var CS "hauler's surplus"
sort TCJ
save nimby_counterfactual, replace


**************************************************************************
****** IMPORT DEMOGRAPHICS IN 3-MILE COMMUNITIES *************************
***************************************************************************
*****************************************************************************
// 1/3. Import demographics for county of origin 
use $data\demographics\raw\county_demographics_2010_CA, clear
keep *geoid *name *pop *white* *black* *hispanic* *asian* *mdhhinc
foreach x of varlist _all {
	rename `x' O_`x'
} 
rename O_COname county
gen year=2010
sort year county
tempfile tmp
save `tmp', replace
use nimby_counterfactual, clear
sort year county
merge m:1 year county using `tmp'
erase `tmp'
drop if _merge==2 //_merge==1 are not 2010, _merge==2 are counties that doesn't generate trash in 2010
drop _merge
replace O_COmdhhinc=O_COmdhhinc*index2000
save nimby_counterfactual, replace

// 2/3. Import demographics for county of destination 
use $data\demographics\raw\county_demographics_2010_CA, clear
keep *geoid *name *pop *white* *black* *hispanic* *asian* *mdhhinc
foreach x of varlist _all {
	rename `x' D_`x'
} 
rename D_COname descnty
gen year=2010
sort year descnty
tempfile tmp
save `tmp', replace
use nimby_counterfactual, clear
sort year descnty
merge m:1 year descnty using `tmp'
erase `tmp'
*list facility descnty if _merge==1 //must be oos
*list  year county facility descnty if _merge==2 // San Francisco, Alpine, Nevada (don't have any landfills) Yuba and Sutter since both merge
drop if _merge==2 //_merge==1 are not 2010, _merge==2 are counties that doesn't generate trash in 2010
drop _merge
replace D_COmdhhinc=D_COmdhhinc*index2000
save nimby_counterfactual, replace

// 3/3. Import demographics for community of destination
use $data\buffer\demographics_buffer_3mile, clear
keep code year Bpop B0pop Bwhite Bblack Basian Bhispanic Bmdhhinc
local varl "white black asian hispanic"
foreach v in `varl' {
  gen B`v'_p=B`v'*100/Bpop
}
foreach x of varlist _all {
	rename `x' D_`x'
} 
rename D_code Code
rename D_year year
sort year Code
tempfile tmp
save `tmp', replace
use nimby_counterfactual, clear
sort year Code
merge m:1 year Code using `tmp'
erase `tmp'
*list facility descnty if _merge==1 //must be oos
*list  year county facility descnty if _merge==2 // San Francisco, Alpine, Nevada (don't have any landfills) Yuba and Sutter since both merge
drop if _merge==2 //_merge==1 must be 0 if analyzing at other years, _merge==2 are counties that doesn't generate trash in 2010
drop _merge
replace D_Bmdhhinc=D_Bmdhhinc*index2000 if year==2010  // adjust income to 2000 dollars
save nimby_counterfactual, replace
************************************************************************
***** SUMMARY STATISTICS OF DEMOGRAPHICS IN 3-MILE COMMUNITIES *******
******** TABLE 5 ****************************************************
use nimby_counterfactual, clear
preserve
keep if oos==0
bys year Code: gen dupj=cond(_N==1,0,_n)
bys year county: gen dupc=cond(_N==1,0,_n)
bys year descnty: gen dupd=cond(_N==1,0,_n)
bys year county: egen qGenerate=total(q)

**total receiving trash, by county
bys year desco: egen qReceive_del=total(q)
gen qReceive1_del=qReceive_del if county==desco // spread values from descnty to county
sort year county
by year county: egen qReceive=mean(qReceive1_del)
replace qReceive=0 if qReceive==.
drop *_del

// Columns 1 and 2 (affected community)
sum D_Bpop D_Bwhite D_Bblack D_Basian D_Bhispanic ///
	D_Bwhite_p D_Bblack_p D_Basian_p D_Bhispanic_p D_Bmdhhinc if year==2010 & dupj<=1
sum D_Bpop D_Bwhite D_Bblack D_Basian D_Bhispanic ///
	D_Bwhite_p D_Bblack_p D_Basian_p D_Bhispanic_p D_Bmdhhinc [iw=q] if year==2010 
// Columns 3 and 4 (receiving county)
sum D_COpop D_COwhite D_COblack D_COasian D_COhispanic D_COwhite_p ///
 D_COblack_p D_COasian_p D_COhispanic_p D_COmdhhinc if year==2010 & dupd<=1
sum O_COpop O_COwhite O_COblack O_COasian O_COhispanic O_COwhite_p ///
 O_COblack_p O_COasian_p O_COhispanic_p O_COmdhhinc [iw=qReceive] if year==2010 & dupc<=1

// Columns 5 and 6 (generating county)
sum O_COpop O_COwhite O_COblack O_COasian O_COhispanic O_COwhite_p ///
 O_COblack_p O_COasian_p O_COhispanic_p O_COmdhhinc  if year==2010  & dupc<=1 & qGenerate>0
sum O_COpop O_COwhite O_COblack O_COasian O_COhispanic O_COwhite_p ///
 O_COblack_p O_COasian_p O_COhispanic_p O_COmdhhinc [iw=qGenerate] if year==2010 & dupc<=1

restore
***** CURRENT DISTRIBUTION OF WASTE DISPOSAL BY RACE ****************
****** TABLE 6 *******************************************************
cap gen D_Bmdhhinc000=D_Bmdhhinc/1000
sum D_Bpop // check if there is affected community noone lives in
local r = r(min)
if `r'==0 {
replace D_Bmdhhinc=0 if D_Bmdhhinc==.
replace D_Bmdhhinc000=0 if D_Bmdhhinc000==.
replace D_Bblack_p=0 if D_Bblack_p==.
replace D_Bwhite_p=0 if D_Bwhite_p==.
replace D_Bhispanic_p=0 if D_Bhispanic_p==.
replace D_Basian_p=0 if D_Basian_p==.
}

eststo clear
tobit share00 D_Bblack_p D_Bhispanic_p D_Basian_p i.T i.C if year==2010 [pw=mktsize], ll vce(cluster J)
eststo mod1
tobit share00 D_Bblack_p D_Bhispanic_p D_Basian_p D_Bmdhhinc000 i.T i.C if year==2010  [pw=mktsize], ll vce(cluster J)
eststo mod2
tobit share00 D_Bblack_p D_Bhispanic_p D_Basian_p D_Bmdhhinc000 realprice i.T i.C if year==2010 [pw=mktsize], ll vce(cluster J)
eststo mod3
tobit share00 D_Bblack_p D_Bhispanic_p D_Basian_p D_Bmdhhinc000 realprice dridis i.T i.C if year==2010 [pw=mktsize], ll vce(cluster J)
eststo mod4
tobit share00 D_Bblack_p D_Bhispanic_p D_Basian_p D_Bmdhhinc000 realprice dridis  Access2 i.T i.C if year==2010 [pw=mktsize], ll vce(cluster J)
eststo mod5
tobit share00 D_Bblack_p D_Bhispanic_p D_Basian_p D_Bmdhhinc000 realprice dridis  Access2 Operation1 i.T i.C if year==2010 [pw=mktsize], ll vce(cluster J)
eststo mod6
tobit share00 D_Bblack_p D_Bhispanic_p D_Basian_p D_Bmdhhinc000 realprice dridis Access2 Operation1 DaysYear i.T i.C if year==2010 [pw=mktsize], ll vce(cluster J)
eststo mod7

esttab mod1 mod2 mod3 mod4 mod5 mod6 mod7 using "$result\table6.tex" , replace  se(2) ar2 nonotes scalars(N_unc ll) compress b(2) nogaps  ///
		title("Tobit share with origin cnty FE, weighted by market size") ///
				mtitle() ///
		star(* 0.10 ** 0.05 *** 0.01)  ///
		drop(_cons *T* *C*) ///
		prefoot(\hline  "quarter FE & Y  & Y & Y & Y  & Y & Y & Y & Y  " \\ ///
					"ori cnty FE & Y  & Y & Y & Y  & Y & Y & Y & Y  " \\ \hline ) 

